const express = require('express');
const router = express.Router();
const db = require('../db.js');

//获取文章
router.get('/getMessage',(req,res)=>{
  let sql = db.getMessage();
  db.Query(sql).then(data=>{
    //在数据控能够查找到结果时，将token发送给前台
    if(data.length){
      console.log('获取文章成功'+data[0].content);
      res.send({"code":"200","sucess":"获取成功！","data":data});
    }else{
      res.send({"code":"401","error":"获取失败"});
    }
    },err=>{
    res.send({"code":"401","error":"获取失败"});
  })
})

//高亮在学单词
router.get('/highlight',(req,res)=>{
  let sql = db.highlight();
  db.Query(sql).then(data=>{
    //在数据控能够查找到结果时，将token发送给前台
    if(data.length){
      console.log('获取在学单词id成功');
      res.send({"code":"200","sucess":"获取成功！","data":data});
    }else{
      res.send({"code":"401","error":"获取失败"});
    }
    },err=>{
    res.send({"code":"401","error":"获取失败"});
  })
})

//翻译单词
router.post('/transWord',(req,res)=>{
  let word=req.body.word;
  console.log(word);
  let sql=db.transWord(word);
  db.Query(sql).then(data=>{
    //在数据控能够查找到结果时，将token发送给前台
    if(data.length){
      console.log('翻译成功！'+data);
      res.send({"code":"200","sucess":"翻译成功！","data":data});
    }else{
      res.send({"code":"401","error":"翻译失败！"});
    }
    },err=>{
    res.send({"code":"401","error":"翻译失败！"});
  })
});



console.log('message接口就绪');
module.exports = router;